<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      canvas {
        border: 1px solid #ccc;
        margin-right: 5px;
      }
    </style>
  </head>
  <body>
    <script>
      (() => {
        const canvas = document.createElement("canvas");
        canvas.width = 400;
        canvas.height = 400;
        document.body.append(canvas);

        const ctx = canvas.getContext("2d");

        ctx.beginPath();
        ctx.strokeRect(100,100,200,200);

        ctx.beginPath();
        ctx.moveTo(100,200);
        ctx.lineTo(200,100);
        ctx.lineTo(300,200);
        ctx.lineTo(200,300);
        ctx.closePath();
        ctx.clip();

        ctx.beginPath();
        ctx.fillStyle = '#00f';
        ctx.fillRect(100,100,200,200);

      })();

      (() => {
        const canvas = document.createElement("canvas");
        canvas.width = 400;
        canvas.height = 400;
        document.body.append(canvas);

        const ctx = canvas.getContext("2d");

        const img = new Image();
        img.src = '../imgs/04.png';
        img.onload = function(){

            ctx.beginPath();
            ctx.arc(200,200,100,0,Math.PI*2);
            ctx.clip();

            ctx.drawImage(img,200-img.width/2 , 200-img.height/2);
        }

      })();

      (() => {
        const canvas = document.createElement("canvas");
        canvas.width = 400;
        canvas.height = 400;
        document.body.append(canvas);

        const ctx = canvas.getContext("2d");
        ctx.strokeRect(100,100,200,200);

        ctx.beginPath();
        ctx.arc(200,200,100,0,Math.PI*2,false); //顺时针
        ctx.arc(200,200,80,0,Math.PI*2,true); //逆时针
        ctx.clip('nonzero');

        ctx.fillRect(100,100,200,200);

      })();

      (() => {
        const canvas = document.createElement("canvas");
        canvas.width = 400;
        canvas.height = 400;
        document.body.append(canvas);

        const ctx = canvas.getContext("2d");
        ctx.strokeRect(100,100,200,200);

        ctx.beginPath();
        ctx.arc(200,200,100,0,Math.PI*2,false); //顺时针
        ctx.arc(200,200,80,0,Math.PI*2,false); //顺时针
        ctx.clip('evenodd');

        ctx.fillRect(100,100,200,200);

      })();
    </script>
  </body>
</html>
